/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __PLANE_H__
#define __PLANE_H__

#include "math/vector.h"
#include "math/ray.h"
#include "material/material.h"
#include "object.h"

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <boost/tuple/tuple.hpp>

using namespace boost::tuples;

namespace Sirat {

namespace Object {

/**
 * Plane class.
 * Defined by a point and a normal.
 */
	
class Plane : public Object {
public:
	/**
	 * Simple constructor.
	 * @param  _material
	 * @param  _point
	 * @param  _normal
	 */
	Plane(Material::Object_Material* _object_material,const Math::Vector& _point,const Math::Vector& _normal);
	
	/**
	 * Default constructor.
	 * Boost Serialization needs a default constructor.
	 * @see Plane(const Plane&)
	 */
	Plane();
	/**
	 * Copy constructor.
	 * @param _plane 
	 */
	Plane(const Plane& _plane);
	/**
	 * Destructor
	 */
	~Plane();

	/**
	 * Normal vector at any given point.
	 * @param _point
	 * @return Plane's normal vector.
	 */
	virtual Math::Vector get_normal(const Math::Vector& _point) const;
	
	/**
	 * @return Plane's point.
	 */
	Math::Vector get_point() const;

	/**
	 * Change plane's normal.
	 * @param _normal
	 */
	void set_normal(const Math::Vector& _normal);
	
	/**
	 * Change plane's point.
	 * @param _point
	 */
	void set_point(const Math::Vector& _point);

	/**
	 * Main function.
	 * @param _ray 
	 * @return Intersection point and the distance from ray's origin.
	 */
	virtual tuple<double,Math::Vector,Object*> intersection(const Math::Ray&);
	
	Plane& operator=(const Plane& _plane);

	Object* clone() { return new Plane(*this); }
	
private:
	friend class boost::serialization::access;
	template<class Archive> void serialize(Archive&, const unsigned int);
	
	Math::Vector point;
	Math::Vector normal;
};



template<class Archive> void Plane::serialize(Archive& ar, const unsigned int /* file_version */){
	ar	& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Object)
			& BOOST_SERIALIZATION_NVP(point)
			& BOOST_SERIALIZATION_NVP(normal);
}

}

}

#endif
